package com.bcx.controller;

import com.bcx.pojo.Entry;
import com.bcx.service.EntryService;
import com.bcx.tool.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class EntryController {
    @Resource
    EntryService entryService;
    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Entry>>selectEntry(@RequestParam(value = "categoryid",required = false,defaultValue = "0")Integer categoryid
    ,@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum
    ){
    PageInfo<Entry> pageInfo=entryService.selectAll(categoryid,pageNum);
    return ResultEntity.successWithoutData(pageInfo);
    }
    //前端请求post,参数是json必须添加RequestBody
    @RequestMapping("/saveEntry")
    public ResultEntity<String> saveEntry(@RequestBody Entry entry){
        try {
            Integer integer=0;
            if (entry.getId()==null){
                integer=entryService.insert(entry);
            }else {
                integer=entryService.update(entry);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }

    }
    //根据id查询
    @RequestMapping("/getEntryById")
    public ResultEntity<Entry>getEntryById(Integer id){
        Entry entry=entryService.getById(id);
        return ResultEntity.successWithoutData(entry);
    }
    //根据id删除
    @RequestMapping("/delEntryById")
    public ResultEntity<String> delEntryById(Integer id){
        try {
            Integer integer=entryService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }

    }

}
